package org.lib.rabbitmqhello.try1;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.lib.rabbitmqhello.config.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * 试验1接口：默认及DirectExchange的使用
 */
@RestController
@RequestMapping(value="/try1")
@Slf4j
public class Try1Controller {

	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	/**
	 * 使用 交换机ext1 发送消息到 队列queue1
	 * @author ben
	 * @date 2021-08-27 21:24:19 CST
	 * @return
	 */
	@GetMapping(value="/send")
	public boolean send() {
		IntStream.range(0, 10).forEach(i->{
			// ex1交换机=>queue1
			rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_1, RabbitConfig.QUEUE_1, 
					"Try1Controller-ex1-Simple msg-" + i + ", @" + new Date());
			
			try {
				TimeUnit.SECONDS.sleep(1L);
			} catch (InterruptedException e) {
				log.info("异常：{}", e.getMessage());
			}
		});
		
		return true;
	}
	
}
